import httplib
import os
import Queue
import threading
import urllib2

from HTMLParser import HTMLParser


def download_image(index, image_url, folder_name):
    file_name = '%03d' % index + '.' + image_url.split('.')[-1]
    full_file_path = folder_name + '//' + file_name

    if not os.path.exists(full_file_path):
        #while True:
            try:
                f = open(full_file_path, 'wb')
                f.write(urllib2.urlopen(image_url, timeout=60).read())
                f.close()
                print 'Downloaded %s to file %s' % (image_url, file_name)
                #break
            except (urllib2.URLError, httplib.BadStatusLine):
                print "Can't download %s, will retry" % image_url
                pass


class ComicBookDownloader:
    def __init__(self, comic_book):
        self.comic_book = comic_book

    def download(self):
        folder_name = self.comic_book.title
        if not os.path.exists(folder_name):
            os.mkdir(folder_name)

        queue = Queue.Queue()

        for x in range(8):
            worker = DownloadWorker(queue, folder_name)
            # Setting daemon to True will let the main thread exit even though the workers are blocking
            worker.daemon = True
            worker.start()

        for index, url in enumerate(self.comic_book.images_url):
            queue.put((index, url))

        queue.join()


class DownloadWorker(threading.Thread):
    def __init__(self, queue, title):
        threading.Thread.__init__(self)
        self.queue = queue
        self.title = title

    def run(self):
        while True:
            index, url = self.queue.get()

            while True:
                try:
                    req = urllib2.Request(url, headers={'User-Agent': "Magic Browser"})
                    con = urllib2.urlopen(req)
                    s = con.read()
                    parser = ComicBookImageParser()
                    parser.feed(s)

                    download_image(index, parser.image_download_url, self.title)
                    self.queue.task_done()
                    break
                except (urllib2.URLError, httplib.BadStatusLine):
                    print "Can't download image %s, will ignore it" % url
                    self.queue.task_done()

class ComicBookImageParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.image_download_url = ''

    def handle_starttag(self, tag, attrs):
        if tag == 'img':
            img_url = ''
            is_download_url = False

            for attr in attrs:
                if attr[0] == 'id' and attr[1] == 'img':
                    is_download_url = True
                elif attr[0] == 'src':
                    img_url = attr[1]

            if is_download_url:
                self.image_download_url = img_url
